package com.shopmall.goods.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.shopmall.goods.dao.GoodsClassifyMapper;
import com.shopmall.goods.entity.GoodsClassify;
import com.shopmall.goods.service.IGoodsClassifyService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsClassifyServiceImpl extends ServiceImpl<GoodsClassifyMapper, GoodsClassify> implements IGoodsClassifyService {
    @Override
    public List<GoodsClassify> classifyList(GoodsClassify classify) {
        LambdaQueryWrapper<GoodsClassify> wrapper = new LambdaQueryWrapper<>();
        if(classify.getClassifyName()!=null || classify.getStatus()!=null){
            wrapper.like(GoodsClassify::getClassifyName,classify.getClassifyName());
            wrapper.eq(GoodsClassify::getStatus,classify.getStatus());
        }else{
            wrapper.eq(GoodsClassify::getPid,0);
        }
        List<GoodsClassify> goodsClassifies = this.baseMapper.selectList(wrapper);
        for (GoodsClassify goodsClassify : goodsClassifies) {
            if("0".equals(goodsClassify.getPid())){
                goodsClassify.setDisabled(false);
            }else{
                goodsClassify.setDisabled(true);
            }
            LambdaQueryWrapper<GoodsClassify> childWrapper = new LambdaQueryWrapper<>();
            childWrapper.eq(GoodsClassify::getPid,goodsClassify.getId());
            List<GoodsClassify> goodsClassifies1 = this.baseMapper.selectList(childWrapper);
            for (GoodsClassify goodsClassify1 : goodsClassifies1) {
                if("0".equals(goodsClassify.getPid())){
                    goodsClassify1.setDisabled(false);
                }else{
                    goodsClassify1.setDisabled(true);
                }
            }
            goodsClassify.setChildren(goodsClassifies1);
        }
        return goodsClassifies;
    }


    @Override
    public int addOrUpdateClassify(GoodsClassify classify) {
        int num=0;
        if(classify.getId()==null){
            classify.setCreateTime(DateUtil.formatDateTime(new Date()));
            num = this.baseMapper.insert(classify);
        }else{
            classify.setUpdateTime(DateUtil.formatDateTime(new Date()));
            num = this.baseMapper.updateById(classify);
        }
        return num;
    }

    @Override
    public int delClassIfyById(Integer id) {
        return 0;
    }

    @Override
    public List<GoodsClassify> getClassify() {
        QueryWrapper<GoodsClassify> query = new QueryWrapper<>();
        query.lambda().eq(GoodsClassify::getPid, 0);
        return baseMapper.selectList(query);
    }
}
